﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Category
{
    public partial class Add : AdminPage
    {
        private CategoryInfo model = null;
        private Category curCategory = new Category();

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
            string ClassNo = txtClassNo.Text.Trim();     //PlugNT.Common.Utils.GetDateTimeCode();
            string ClassName = this.txtClassName.Text.Trim();               //栏目名称
            string ClassList = "";                                          //栏目包含列表
            string ClassPre = this.DdlMenu.SelectedValue.Trim();            //上一级目录
            string parentCreatepath = "";
            int clsListLen = 1;                                             //栏目深度

            if (ClassNo!="" && ClassName != "")
            {
                if (ClassPre == "0")
                {
                    ClassList = ClassNo + ",";
                }
                else
                {
                    DataTable dt = curCategory.GetListRow(ClassPre);
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        ClassList = dr["clslist"].ToString().Trim() + ClassNo + ",";
                        clsListLen = Convert.ToInt32(dr["clslistlen"]) + 1;
                    }
                }
                model = new CategoryInfo();
                model.clsno = ClassNo;
                model.clsname = ClassName;
                model.clslist = ClassList;
                model.clsparentno = ClassPre;
                model.clslistlen = clsListLen;

                if (curCategory.Add(model))
                {
                    Response.Redirect("Admin.aspx");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('添加栏目失败！栏目编号已存在！')", true);
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('栏目保存操作失败！栏目编号或栏目名或生成目录为空！')", true);
            }
            

        }



        #region 数据绑定
        private void BindData()
        {

            this.btnAdd.Attributes.Add("onclick", "return ChkInput()");
            DataTable dt = curCategory.GetOrderList();

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("添加为根类别", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                int clsListLen = Convert.ToInt32(dr["clslistlen"]);
                string ClassNo = dr["clsno"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();

                if (clsListLen == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassNo));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(clsListLen - 1, "　") + ClassName;
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassNo));
                }
            }
            this.DdlMenu.Attributes.Add("onchange", "SwapText();");
        }
        #endregion



    }
}
